package nc.ui.so.m30.billui.editor.bodyevent;

import nc.bs.framework.common.NCLocator;
import nc.itf.price.priceform.IPriceFormService;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pubapp.uif2app.event.IAppEventHandler;
import nc.ui.pubapp.uif2app.event.card.CardBodyAfterEditEvent;
import nc.ui.so.m30.billui.view.SaleOrderBillForm;
import nc.ui.so.pub.keyvalue.CardKeyValue;
import nc.vo.price.pub.entity.PriceFormAggVO;
import nc.vo.price.pub.entity.PriceFormBVO;
import nc.vo.pub.BusinessException;
import nc.vo.so.m30.entity.SaleOrderBVO;

public class BodyAfterEditGetPriceFormHandler implements
		IAppEventHandler<CardBodyAfterEditEvent> {
	
	public BodyAfterEditGetPriceFormHandler() {
	}
	
	private SaleOrderBillForm billform;

	public SaleOrderBillForm getBillform() {
		return this.billform;
	}

	@Override
	public void handleAppEvent(CardBodyAfterEditEvent e) {
		BillCardPanel billCardPanel = this.billform.getBillCardPanel();
		CardKeyValue keyValue = new CardKeyValue(billCardPanel);
		int row = e.getRow();
		String priceFormID =
	        keyValue.getBodyStringValue(row, SaleOrderBVO.CPRICEFORMID);
		if(priceFormID == null) return;
		IPriceFormService ipfs = NCLocator.getInstance().lookup(IPriceFormService.class);
		PriceFormAggVO priceFormAggVO = null;
		try {
			priceFormAggVO = ipfs.queryPriceFormByPK(priceFormID);
		} catch (BusinessException e1) {
			e1.printStackTrace();
		}
		
		if(priceFormAggVO != null)
		{
			PriceFormBVO[] children = (PriceFormBVO[])priceFormAggVO.getChildren(PriceFormBVO.class);
			java.util.List<Integer> list = new java.util.ArrayList<Integer>();
			for(int i = 0; i < children.length; i++)
			{
				int fformtypeflag = children[i].getFformtypeflag();
				
				if(fformtypeflag == 4)
				{
					keyValue.setBodyValue(row, "vbdef15", "Y");
				}
				list.add(fformtypeflag);
			}
			if(!list.contains(4))
			{
				keyValue.setBodyValue(row, "vbdef15", "N");
			}
		}
	}

	public void setBillform(SaleOrderBillForm billform) {
		this.billform = billform;
	}
}
